home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 446 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  2. Message-ID: <4giddp$7q6@mulga.cs.mu.OZ.AU>
  3. X-Original-Date: 22 Feb 1996 18:42:01 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 22 Feb 96 20:20:17 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Article for comp.std.c++: Eliminating #ifdef: if const
  9. Organization: Comp Sci, University of Melbourne
  10. References: <199602160807.IAA06126@condor.ukc.ac.uk> <4gb0a4$sa3@fido.asd.sgi.com>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMSzQD+EDnX0m9pzZAQH2BQF+MeSYzR9OwhgOR7uS276YQofZUnI+MPPd
  13.     6xq5rGngDUvmpiJ6PRuumWtxZpYIMlBB
  14.     =PKxA
  15.  
  16. shankar@mti.mti.sgi.com (Shankar Unni) writes:
  17.  
  18. >Often, an #ifdef covers just a small part of an expression that may be
  19. >different between different environments, or some such small differences:
  20. >
  21. >   if (some condition) {
  22. >#ifdef SOMEIMPL
  23. >    if (some other condition) {
  24. >#endif /* SOMEIMPL */
  25. >    /* code */
  26. >    /* code */
  27. >    /* code */
  28. >#ifdef SOMEIMPL
  29. >    }
  30. >#endif /* SOMEIMPL */
  31. >   }
  32.  
  33. But with `if const', your 11 lines of code could be rewritten as either
  34.  
  35.     if (some condition) {
  36.         if (SOMEIMPL && some other condition) {
  37.             /* code */
  38.             /* code */
  39.             /* code */
  40.         }
  41.     }
  42.  
  43.     (7 lines of code)
  44.  
  45. or, if `some other condition' won't compile on other implementations, as
  46.  
  47.     if (some condition) {
  48.         bool some_other_condition = true;
  49.         if const (SOMEIMPL) {
  50.             some_other_condition = some other condition;
  51.         }
  52.         if (other_condition) {
  53.             /* code */
  54.             /* code */
  55.             /* code */
  56.         }
  57.     }
  58.  
  59.     (11 lines of code)
  60.  
  61. Both of these solutions have the advantage that the test for `SOMEIMPL'
  62. occurs only once, so things will be easier if you later have to change it to 
  63. `SOMEIMPL || SOMEOTHERIMPL'.
  64.  
  65. >Duplicating all the code so that each half is precisely correct in its {}
  66. >matching is both tedious and error-prone (trying to verify that both parts
  67. >of the expression are fixed).
  68.  
  69. But fortunately as shown above it would not be required.
  70.  
  71. >Also, the #ifdefs serve a purpose in pointing out the fact that this piece
  72. >of code is system-dependent.  The syntax you propose hides its warts rather
  73. >too well, and make it easy for a reader to miss the significant piece of
  74. >information that this area of code is system-dependent.
  75.  
  76. True, but a convention of using upper-case for SOMEIMPL and similar constants
  77. would make such tests stand out.
  78.  
  79. --
  80. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  81. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  82. ---
  83. [ To submit articles: try just posting with your news-reader.
  84.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  85.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  86.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  87.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  88. ]
  89.